package com.lxyk.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxyk.pojo.Customer;
import com.lxyk.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/cust")
@Slf4j
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    /**
     * 查询所有客户信息
     * @param model
     * @return
     */
    //淘汰了 因为我们要查询带分页
   /* @RequestMapping("/listCust")
    public String listCust(Model model){
        List<Customer> custList = customerService.listCustomer();
        model.addAttribute("custList",custList);
        return "customerList";
    }*/

    /**
     * 查询所有带分页
     * @param pageNum
     * @param pageSize
     * @param model
     * @param customer
     * @return
     */
    @RequestMapping("/listCust")
    public String listCust(@RequestParam(required = false,defaultValue = "1",value = "pageNum")Integer pageNum,
                           @RequestParam(defaultValue = "10",value = "pageSize")Integer pageSize, Model model,Customer customer){

        if (pageNum <= 0 || pageNum.equals("") || pageNum == null){
            pageNum = 1;
        }
        if (pageSize <= 0 || pageSize.equals("") || pageSize == null){
            pageSize = 10;
        }
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Customer> pageInfo = customerService.listCust(customer, pageNum, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        return "customerList";
    }

    /**
     * 点击“添加”按钮跳转到“添加客户”页面
     * @return
     */
    @RequestMapping("/preSaveCust")
    public String preSaveCust(){
        return "custSave";
    }

    /**
     * 添加客户信息，将表单输入的客户信息持久化到数据库中，添加成功返回客户列表页面
     * @param customer
     * @return
     */
    @PostMapping("/saveCust")
    public String saveCust(Customer customer) {
        log.info("Customer = "+customer);//必须有@Slf4j注解才能使用log
        int rows = customerService.saveCustomer(customer);
        log.info("save customer row = "+rows);
        // 重定向到客户列表，将新添加的客户信息显示到客户列表
        return "redirect:/cust/listCust";
    }

    @RequestMapping("/delCust/{cid}")//这里要和前端联系起来 由前端得到你点击删除的那一行的id并传到这里
    public String deleteCustById(@PathVariable("cid") Integer cid){
        int byId = customerService.deleteCustomerById(cid);
        log.info("byId = "+byId);
        return "redirect:/cust/listCust";
    }
    /**
     * 根据id查询  数据回显
     * @param cid
     * @param model
     * @return
     */
    @RequestMapping("/preUpdateCust/{cid}")
    public String preCustomerById(@PathVariable("cid") Integer cid,Model model){
        Customer customer = customerService.getCustomerById(cid);
        model.addAttribute("customer",customer);
        return "customerUpdate";
    }

    /**
     * 修改客户信息
     * @param customer
     * @return
     */
    @PostMapping("/updateCust")
    public String  updateCust(Customer customer){
        customerService.updateCustomer(customer);
        return "redirect:/cust/listCust";
    }
}
